//
//  RootViewController.m
//  UI4_事件_手势
//
//  Created by dllo on 15/11/11.
//  Copyright (c) 2015年 dllo. All rights reserved.
//

#import "RootViewController.h"

@interface RootViewController ()

@property (nonatomic, retain) UILabel *label;

@end

@implementation RootViewController

- (void)dealloc
{
    [_label release];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.label = [[UILabel alloc] initWithFrame:CGRectMake(100, 250, 200, 200)];
    self.label.backgroundColor = [UIColor whiteColor];
    self.label.text = @"🐷";
    self.label.font = [UIFont systemFontOfSize:200];
    self.label.userInteractionEnabled = YES;
    [self.view addSubview:self.label];
    [_label release];
    
    // 轻拍手势
    UITapGestureRecognizer *tapGR = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];
    
    // 为某一视图添加手势
    [self.view addGestureRecognizer:tapGR];
    [self.label addGestureRecognizer:tapGR];
    // 轻拍次数
    tapGR.numberOfTapsRequired = 3;
    // 手指数 - 模拟器按住alt键来模拟两个手指
    tapGR.numberOfTouchesRequired = 2;
    [tapGR release];
    
    
    // 长按手势
    UILongPressGestureRecognizer *longGR = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longAction:)];
    [self.label addGestureRecognizer:longGR];
    [longGR release];
    
    
    // 平移
//    UIPanGestureRecognizer *panGR = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panAction:)];
//    [self.view addGestureRecognizer:panGR];
//    [panGR release];
    
    
    // 旋转
    UIRotationGestureRecognizer *rotationGR = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotationAction:)];
    [self.label addGestureRecognizer:rotationGR];
    [rotationGR release];
    
    
    // 捏合
    UIPinchGestureRecognizer *pinchGR = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinchAction:)];
    [self.label addGestureRecognizer:pinchGR];
    [pinchGR release];
    
    
    // 轻扫
    UISwipeGestureRecognizer *swipeGR = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeAction:)];
    [self.view addGestureRecognizer:swipeGR];
    [swipeGR release];
    
    
    // 边缘轻扫
    UIScreenEdgePanGestureRecognizer *screenGR = [[UIScreenEdgePanGestureRecognizer alloc] initWithTarget:self action:@selector(screenAction:)];
    screenGR.edges = UIRectEdgeLeft;
    [self.view addGestureRecognizer:screenGR];
    [screenGR release];
    
}

- (void)screenAction:(UIScreenEdgePanGestureRecognizer *)sender
{
    NSLog(@"边缘轻扫");
}

- (void)swipeAction:(UISwipeGestureRecognizer *)sender
{
    NSLog(@"轻扫");
    self.label.center = CGPointMake(arc4random() % 200, arc4random() %500);
}




- (void)pinchAction:(UIPinchGestureRecognizer *)sender
{
    NSLog(@"捏合手势");
    self.label.transform = CGAffineTransformScale(self.label.transform, sender.scale, sender.scale);
    sender.scale = 1;


}

- (void)rotationAction:(UIRotationGestureRecognizer *)sender
{
    NSLog(@"旋转手势");
    // 将label的旋转角度设置为手势的旋转角度
    self.label.transform = CGAffineTransformRotate(self.label.transform, sender.rotation);
    // 注意, 每次清0一下旋转角度, 否则会不断累积啊
    sender.rotation = 0;
}


- (void)panAction:(UIPanGestureRecognizer *)sender
{
    NSLog(@"平移手势");
    CGPoint point = [sender translationInView:self.view];
    NSLog(@"%f %f", point.x, point.y);
    
    self.view.backgroundColor = [UIColor colorWithRed:point.x / 255.0 green:point.y / 255.0 blue:(point.x + point.y) / 255.0 alpha:1.0];
}

- (void)longAction:(UILongPressGestureRecognizer *)sender
{
//    NSLog(@"长按手势");
    
    // 按下, 抬起, 拖动
    if (UIGestureRecognizerStateChanged == sender.state) {
        NSLog(@"长按拖动");
        
    // 返回相对于self.view的坐标
        CGPoint point = [sender locationInView:self.view];
    // 将label的中心点坐标设置为手指触摸屏幕时时改变的坐标
        self.label.center = point;
        
    }
    if (UIGestureRecognizerStateBegan == sender.state) {
        NSLog(@"长按按下");
    }
    
}


- (void)tapAction:(UITapGestureRecognizer *)sender
{
    self.label.text = @"🐯";
}


// 响应者链:时间响应顺序 - 硬件 -> 系统 -> APP -> UIWindow -> 再根据触点的坐标向最顶端视图一级一级传递.  若最顶端视图不能响应, 再向后传递, 直到遇到一个能响应的视图响应并结束事件的传递, 若无任何一个视图能响应事件, 则丢弃事件.

// 可通过userInteractionEnabled 开/关视图对事件的响应能力
// 注意:1, 若关闭视图对事件的响应能力, 则其子视图及子视图的子视图将全部不会再具备响应事件的能力;  2, UIImageView和UILabel的默认事件响应能力是关闭的!

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    NSLog(@"触摸开始");
}

-(void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
{
    NSLog(@"触摸取消");
}

-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    NSLog(@"触摸结束");
}

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    NSLog(@"触摸移动");
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event
{
    NSLog(@"摇一摇/晃动开始");
}

-(void)motionCancelled:(UIEventSubtype)motion withEvent:(UIEvent *)event
{
    NSLog(@"摇一摇/晃动取消");
}

-(void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event
{
    NSLog(@"摇一摇/晃动结束");
}



/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
